<template>
  <el-container class="app-container">
    <el-header>
      <h2>Token Refresh Demo</h2>
      <div class="user-info" v-if="isLoggedIn">
        <span>{{ username }}</span>
        <el-button type="text" @click="handleLogout">退出</el-button>
      </div>
    </el-header>
    <el-main>
      <router-view></router-view>
    </el-main>
  </el-container>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()
const username = ref(sessionStorage.getItem('username') || '')
const isLoggedIn = computed(() => !!sessionStorage.getItem('accessToken'))

const handleLogout = () => {
  sessionStorage.removeItem('accessToken')
  sessionStorage.removeItem('username')
  document.cookie = 'refreshToken=; Max-Age=0; Path=/;'
  router.push('/login')
}
</script>

<style>
.app-container {
  height: 100vh;
}

.el-header {
  background-color: #409EFF;
  color: white;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 10px;
}

.el-button--text {
  color: white;
}
</style>